package com.kekezu.kppw.data;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.tsz.afinal.FinalDb;

import org.apache.http.NameValuePair;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.kekezu.kppw.bean.UserBean;
import com.kekezu.kppw.utils.ConfigInc;
import com.kekezu.kppw.utils.HttpUtils;

@SuppressWarnings("deprecation")
public class ManuscriptData {
	static FinalDb db;

	/**
	 * 创建稿件
	 * 
	 * @param strDesc
	 * @param strTaskId
	 * @param file_id
	 * @param context
	 */
	public static String[] createWinBid(String strDesc, String strTaskId,
			String file_id, String strPrice, Context context) {
		String strRes[] = new String[2];
		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);
		String urlString = ConfigInc.getServiceAdress(context)
				+ "work/createWinBid";
		HttpUtils httpUtils = HttpUtils.getInstance();

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("desc", strDesc));
		params.add(new BasicNameValuePair("task_id", strTaskId));
		params.add(new BasicNameValuePair("file_id", file_id));
		params.add(new BasicNameValuePair("token", users.get(0).getToken()));
		params.add(new BasicNameValuePair("price", strPrice));

		try {
			String strJson = httpUtils.executeRequest(urlString, params);
			JSONObject jsonObject = new JSONObject(strJson);

			strRes[0] = jsonObject.getString("code");
			strRes[1] = jsonObject.getString("message");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strRes;
	}

	/**
	 * 附件上传
	 * 
	 * @param file_id
	 * @param context
	 * @return
	 */
	public static String fileUpload(String file_id, Context context) {
		String string = null;
		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		String urlString = ConfigInc.getServiceAdress(context) + "fileUpload";
		HttpUtils httpUtils = HttpUtils.getInstance();

		try {
			MultipartEntity multipart = new MultipartEntity();
			multipart.addPart("file", new FileBody(new File(file_id)));
			multipart.addPart("token", new StringBody(users.get(0).getToken(),
					Charset.forName("UTF-8")));

			String strJson = httpUtils.httpPost(urlString, multipart);

			JSONObject jsonObject = new JSONObject(strJson);
			String resString = jsonObject.getString("code");
			String resMsg = jsonObject.getString("message");

			// 对返回的数据进行判断
			if (resString.equals("1000")) {
				JSONObject jsondata = new JSONObject(
						jsonObject.getString("data"));
				string = jsondata.getString("id");
			} else {
				Toast.makeText(context, resMsg, Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return string;
	}

	/**
	 * 稿件详情
	 * 
	 * @param strWordId
	 * @param context
	 * @return
	 */
	public static Map<String, String> getWorkDetails(String strWordId,
			Context context) {
		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);
		Map<String, String> resMap = new HashMap<String, String>();
		String urlString = ConfigInc.getServiceAdress(context) + "work/detail?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		Map<String, String> params = new HashMap<String, String>();
		params.put("id", strWordId);
		params.put("token", users.get(0).getToken());
		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");

			resMap.put("code", jsonObject.getString("code"));
			resMap.put("message", jsonObject.getString("message"));
			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				JSONObject jsondata = new JSONObject(
						jsonObject.getString("data"));

				resMap.put("id", jsondata.getString("id"));// 稿件id
				resMap.put("desc", jsondata.getString("desc"));// 详情
				resMap.put("task_id", jsondata.getString("task_id"));// 任务id
				resMap.put("status", jsondata.getString("status"));// 稿件状态
				resMap.put("uid", jsondata.getString("uid"));// 稿件所属人id
				resMap.put("bid_by", jsondata.getString("bid_by"));// 是否中标
				resMap.put("nickname", jsondata.getString("nickname"));// 昵称
				resMap.put("avatar", jsondata.getString("avatar"));// 图像
				resMap.put("email_status", jsondata.getString("email_status"));
				resMap.put("task_status", jsondata.getString("task_status"));
				resMap.put("task_type", jsondata.getString("task_type"));
				resMap.put("applauseRate", jsondata.getString("applauseRate")
						+ "%");// 好评率
				resMap.put("button_status", jsondata.getString("button_status"));
				resMap.put("complete", jsondata.getString("complete"));
				resMap.put("attachment", jsondata.getString("attachment"));// 附件
				resMap.put("isEnterprise", jsondata.getString("isEnterprise"));
				resMap.put("bank", jsondata.getString("bank"));
				resMap.put("alipay", jsondata.getString("alipay"));
				resMap.put("email", jsondata.getString("email"));
				resMap.put("realname", jsondata.getString("realname"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resMap;
	}

	/**
	 * 中标后交付
	 * 
	 * @param strDesc
	 * @param strTaskId
	 * @param strFile
	 * @param context
	 */
	public static String[] createDelivery(String strDesc, String strTaskId,
			String strFile, String strSort, Context context) {
		String strRes[] = new String[2];
		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		String urlString = ConfigInc.getServiceAdress(context)
				+ "work/createDelivery";
		HttpUtils httpUtils = HttpUtils.getInstance();

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("desc", strDesc));
		params.add(new BasicNameValuePair("task_id", strTaskId));
		params.add(new BasicNameValuePair("file_id", strFile));
		params.add(new BasicNameValuePair("token", users.get(0).getToken()));
		params.add(new BasicNameValuePair("sort", strSort));

		try {
			String strJson = httpUtils.executeRequest(urlString, params);
			JSONObject jsonObject = new JSONObject(strJson);

			strRes[0] = jsonObject.getString("code");
			strRes[1] = jsonObject.getString("message");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strRes;
	}

	/**
	 * 交付稿件维权
	 * 
	 * @param strWorkId
	 * @param intType
	 * @param strDesc
	 * @param strTaskId
	 * @param context
	 */
	public static String[] deliveryRight(String strWorkId, int intType,
			String strDesc, String strTaskId, Context context) {
		String str[] = new String[2];
		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		String urlString = ConfigInc.getServiceAdress(context)
				+ "work/deliveryRight";
		HttpUtils httpUtils = HttpUtils.getInstance();

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("work_id", strWorkId));
		params.add(new BasicNameValuePair("token", users.get(0).getToken()));
		params.add(new BasicNameValuePair("type", "" + intType));
		params.add(new BasicNameValuePair("desc", strDesc));
		params.add(new BasicNameValuePair("task_id", strTaskId));
		try {
			String strJson = httpUtils.executeRequest(urlString, params);
			JSONObject jsonObject = new JSONObject(strJson);

			str[0] = jsonObject.getString("code");
			str[1] = jsonObject.getString("message");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

	/**
	 * 交易评论
	 * 
	 * @param strTaskId
	 * @param comment
	 * @param speed_score
	 * @param quality_score
	 * @param attitude_score
	 * @param strEvlType
	 * @param strWorkID
	 * @param context
	 */
	public static String[] putEvaluate(String strTaskId, String comment,
			float speed_score, float quality_score, float attitude_score,
			String strEvlType, String strWorkID, Context context) {
		String[] str = new String[2];
		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		String urlString = ConfigInc.getServiceAdress(context)
				+ "work/evaluate";
		HttpUtils httpUtils = HttpUtils.getInstance();

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("task_id", strTaskId));
		params.add(new BasicNameValuePair("comment", comment));
		params.add(new BasicNameValuePair("type", "" + strEvlType));
		params.add(new BasicNameValuePair("work_id", strWorkID));
		params.add(new BasicNameValuePair("token", users.get(0).getToken()));
		params.add(new BasicNameValuePair("speed_score", "" + speed_score));
		params.add(new BasicNameValuePair("quality_score", "" + quality_score));
		if (attitude_score > 0) {
			params.add(new BasicNameValuePair("attitude_score", ""
					+ attitude_score));
		}
		try {
			String strJson = httpUtils.executeRequest(urlString, params);
			JSONObject jsonObject = new JSONObject(strJson);
			str[0] = jsonObject.getString("code");
			str[1] = jsonObject.getString("message");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

	/**
	 * 选稿中标
	 * 
	 * @param WorkId
	 * @param context
	 */
	public static String[] putWinBid(String WorkId, Context context) {
		String str[] = new String[2];

		String urlString = ConfigInc.getServiceAdress(context) + "work/winBid?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		Map<String, String> params = new HashMap<String, String>();
		params.put("id", WorkId);
		params.put("token", "" + users.get(0).getToken());

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);

			str[0] = jsonObject.getString("code");
			str[1] = jsonObject.getString("message");

		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

	/**
	 * 交付稿件验收
	 * 
	 * @param WorkId
	 * @param context
	 */
	public static String[] deliveryAgree(String WorkId, String strStatus,
			Context context) {
		String str[] = new String[2];

		String urlString = ConfigInc.getServiceAdress(context)
				+ "work/deliveryAgree?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		Map<String, String> params = new HashMap<String, String>();
		params.put("work_id", WorkId);
		params.put("token", "" + users.get(0).getToken());
		params.put("status", strStatus);
		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);

			str[0] = jsonObject.getString("code");
			str[1] = jsonObject.getString("message");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

	/**
	 * 协议
	 * 
	 * @param strCode
	 * @param context
	 * @return
	 */
	public static String agreementDetail(String strCode, Context context) {
		String string = null;
		String urlString = ConfigInc.getServiceAdress(context)
				+ "agreementDetail?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		Map<String, String> params = new HashMap<String, String>();
		params.put("code_name", strCode);

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");

			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				JSONObject jsondata = new JSONObject(
						jsonObject.getString("data"));

				string = jsondata.getString("agreeInfo");

			} else {
				Toast.makeText(context, "请求异常", Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return string;
	}

	/**
	 * 威客查询交付稿件详情
	 * 
	 * @param strTaskId
	 * @param context
	 * @return
	 */
	public static Map<String, String> guestDelivery(String strTaskId,
			Context context) {
		Map<String, String> resMap = new HashMap<String, String>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "guestDelivery?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		Map<String, String> params = new HashMap<String, String>();
		params.put("task_id", strTaskId);
		params.put("token", users.get(0).getToken());
		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");

			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				JSONObject jsondata = new JSONObject(
						jsonObject.getString("data"));

				resMap.put("gname", jsondata.getString("gname"));// 雇主（甲方）名称
				resMap.put("wname", jsondata.getString("wname"));// 威客（乙方名称）
				resMap.put("desc", jsondata.getString("desc"));// 交付稿件内容
				resMap.put("attachInfo", jsondata.getString("attachInfo"));// 附件
			} else {
				Toast.makeText(context, "请求异常", Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resMap;
	}

	/**
	 * 雇主查询交付稿件详情
	 * 
	 * @param strTaskId
	 * @param Uid
	 * @param context
	 * @return
	 */
	public static Map<String, String> agreeDelivery(String strTaskId,
			String Uid, Context context) {
		Map<String, String> resMap = new HashMap<String, String>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "agreeDelivery?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		Map<String, String> params = new HashMap<String, String>();
		params.put("task_id", strTaskId);
		params.put("id", Uid);
		params.put("token", users.get(0).getToken());
		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");

			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				JSONObject jsondata = new JSONObject(
						jsonObject.getString("data"));

				resMap.put("gname", jsondata.getString("gname"));// 雇主（甲方）名称
				resMap.put("wname", jsondata.getString("wname"));// 威客（乙方名称）
				resMap.put("desc", jsondata.getString("desc"));// 交付稿件内容
				resMap.put("attachInfo", jsondata.getString("attachInfo"));// 附件
			} else {
				Toast.makeText(context, "请求异常", Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resMap;
	}

	/**
	 * 查看评价信息
	 * 
	 * @param strWordId
	 * @param context
	 * @return
	 */
	public static Map<String, String> getEvaluate(String strWordId,
			Context context) {
		Map<String, String> resMap = new HashMap<String, String>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "work/getEvaluate?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		Map<String, String> params = new HashMap<String, String>();
		params.put("work_id", strWordId);
		params.put("token", users.get(0).getToken());
		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");

			Log.e("", jsonObject.toString());

			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				JSONObject jsondata = new JSONObject(
						jsonObject.getString("data"));

				resMap.put("comment", jsondata.getString("comment"));
				resMap.put("type", jsondata.getString("type"));
				resMap.put("speed_score", jsondata.getString("speed_score"));

				if (TestDataData.getuserType(context) == 1) {
					resMap.put("quality_score",
							jsondata.getString("quality_score"));
				}

				resMap.put("attitude_score",
						jsondata.getString("attitude_score"));

				if (jsondata.getString("comment_by").equals("0")) {
					resMap.put("comment_by", "来自威客的评论");
				} else if (jsondata.getString("comment_by").equals("1")) {
					resMap.put("comment_by", "来自雇主的评价");
				} else if (jsondata.getString("comment_by").equals("2")) {
					resMap.put("comment_by", "系统产生的评价");
				}
			} else {
				Toast.makeText(context, "请求异常", Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resMap;
	}
}
